package 剑指Offer.easy;

import java.util.Arrays;

public class 数组中出现次数超过一半的数字 {
    class Solution {
        public int majorityElement2(int[] nums) {
            Arrays.sort(nums);
            return nums[nums.length / 2];
        }

        public int majorityElement(int[] nums) {
            int votes = 0;
            int targetNum = nums[0];
            for (int num : nums) {
                if (votes == 0) {
                    targetNum = num;
                }
                if (num == targetNum) {
                    votes++;
                } else {
                    votes--;
                }
            }
            return targetNum;
        }
    }
}
